package com.sleepcamel.resteasyConfigurator.components;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;

import com.sleepcamel.resteasyConfigurator.model.ListChooserModel;
import com.sleepcamel.resteasyConfigurator.util.StringCaseInsensitiveSort;

@SuppressWarnings("unchecked")
public class TwoListChooser extends Composite{
	
	private ListViewer selectedListViewer;
	private Button add;
	private Button remove;
	private Button removeAll;
	private ListViewer availableListViewer;
	private ListChooserModel<String> listChooserModel;

	public TwoListChooser(Composite parent, int style) {
		super(parent, style);
		setBackground(this);
		listChooserModel = new ListChooserModel<String>();
		createControl();
	}

	private void setBackground(Control control) {
		control.setBackground(Display.getDefault().getSystemColor(
				SWT.COLOR_WHITE));
	}

	private void createControl() {
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		setLayout(layout);

		Label selectedLabel = new Label(this, SWT.NONE);
		selectedLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		setBackground(selectedLabel);
		selectedLabel.setText("Selected");

		Label emptyLabel = new Label(this, SWT.NONE);
		emptyLabel.setText("");

		Label availableLabel = new Label(this, SWT.NONE);
		availableLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		setBackground(availableLabel);
		availableLabel.setText("Available");

		List selectedList = new List(this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		selectedList.setItems(new String[] {});
		GridData data = new GridData(GridData.FILL_BOTH);
		data.heightHint = 150;
		data.widthHint = 170;
		selectedList.setLayoutData(data);
		selectedListViewer = new ListViewer(selectedList);
		selectedListViewer.setContentProvider(ArrayContentProvider.getInstance());
		selectedListViewer.setSorter(new StringCaseInsensitiveSort());
		
		Composite comp = new Composite(this, SWT.NONE);
		setBackground(comp);

		data = new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_FILL);
		data.widthHint = 50;
		comp.setLayoutData(data);

		layout = new GridLayout();
		layout.marginWidth = 5;
		layout.verticalSpacing = 20;
		comp.setLayout(layout);

		add = new Button(comp, SWT.PUSH);
		add.setText("<-");
		add.setLayoutData(new GridData(SWT.HORIZONTAL | SWT.VERTICAL));
		add.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection = (StructuredSelection) availableListViewer.getSelection();
				if ( selection != null && !selection.isEmpty() ){
					listChooserModel.addSelectedChoices(selection.toList());
					selectedListViewer.refresh();
					availableListViewer.refresh();
				}
			}
		});

		remove = new Button(comp, SWT.PUSH);
		remove.setText("->");
		remove.setLayoutData(new GridData(SWT.HORIZONTAL | SWT.VERTICAL));
		remove.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection = (StructuredSelection) selectedListViewer.getSelection();
				if ( selection != null && !selection.isEmpty() ){
					listChooserModel.removeSelectedChoices(selection.toList());
					selectedListViewer.refresh();
					availableListViewer.refresh();
				}
			}
		});

		removeAll = new Button(comp, SWT.PUSH);
		removeAll.setText(">>");
		removeAll.setLayoutData(new GridData(SWT.HORIZONTAL | SWT.VERTICAL));
		removeAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				listChooserModel.removeAllSelectedChoices();
				selectedListViewer.refresh();
				availableListViewer.refresh();
			}
		});
		
		List availableList = new List(this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		data = new GridData(1808);
		data.heightHint = 150;
		data.widthHint = 170;
		availableList.setLayoutData(data);
		availableListViewer = new ListViewer(availableList);
		availableListViewer.setContentProvider(ArrayContentProvider.getInstance());
		availableListViewer.setSorter(new StringCaseInsensitiveSort());
	}

	protected void setChoices(java.util.List<String> allChoices, java.util.List<String> selectedChoices) {
		listChooserModel.setUnSelectedChoices(allChoices);
		listChooserModel.setSelectedChoices(selectedChoices);
		availableListViewer.setInput(allChoices);
		selectedListViewer.setInput(selectedChoices);
	}

}
